package cn.iocoder.yudao.module.lfd.controller.admin.appraise;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.math.RoundingMode;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.lfd.controller.admin.appraise.vo.*;
import cn.iocoder.yudao.module.lfd.dal.dataobject.appraise.AppraiseDO;
import cn.iocoder.yudao.module.lfd.convert.appraise.AppraiseConvert;
import cn.iocoder.yudao.module.lfd.service.appraise.AppraiseService;

@Tag(name = "管理后台 - 评价")
@RestController
@RequestMapping("/lfd/appraise")
@Validated
public class AppraiseController {

    @Resource
    private AppraiseService appraiseService;

    @PostMapping("/create")
    @Operation(summary = "创建评价")
    @PreAuthorize("@ss.hasPermission('lfd:appraise:create')")
    public CommonResult<Long> createAppraise(@Valid @RequestBody AppraiseCreateReqVO createReqVO) {
        return success(appraiseService.createAppraise(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新评价")
    @PreAuthorize("@ss.hasPermission('lfd:appraise:update')")
    public CommonResult<Boolean> updateAppraise(@Valid @RequestBody AppraiseUpdateReqVO updateReqVO) {
        appraiseService.updateAppraise(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除评价")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('lfd:appraise:delete')")
    public CommonResult<Boolean> deleteAppraise(@RequestParam("id") Long id) {
        appraiseService.deleteAppraise(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得评价")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('lfd:appraise:query')")
    public CommonResult<AppraiseRespVO> getAppraise(@RequestParam("id") Long id) {
        AppraiseDO appraise = appraiseService.getAppraise(id);
        return success(AppraiseConvert.INSTANCE.convert(appraise));
    }

    @GetMapping("/list")
    @Operation(summary = "获得评价列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('lfd:appraise:query')")
    public CommonResult<List<AppraiseRespVO>> getAppraiseList(@RequestParam("ids") Collection<Long> ids) {
        List<AppraiseDO> list = appraiseService.getAppraiseList(ids);
        return success(AppraiseConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得评价分页")
    @PreAuthorize("@ss.hasPermission('lfd:appraise:query')")
    public CommonResult<PageResult<AppraiseRespVO>> getAppraisePage(@Valid AppraisePageReqVO pageVO) {
        PageResult<AppraiseDO> pageResult = appraiseService.getAppraisePage(pageVO);
        return success(AppraiseConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出评价 Excel")
    @PreAuthorize("@ss.hasPermission('lfd:appraise:export')")
    @OperateLog(type = EXPORT)
    public void exportAppraiseExcel(@Valid AppraiseExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AppraiseDO> list = appraiseService.getAppraiseList(exportReqVO);
        // 导出 Excel
        List<AppraiseExcelVO> datas = AppraiseConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "评价.xls", "数据", AppraiseExcelVO.class, datas);
    }

    @GetMapping("/mini-summary")
    @Operation(summary = "获得评价")
    @PermitAll
    public CommonResult<Map<String, Object>> getMini(Long shopId) {
        PageResult<AppraiseDO> pageResult = appraiseService.getAppraisePage(new AppraisePageReqVO().setShopId(shopId));
        List<AppraiseRespVO> appraiseRespVOS = AppraiseConvert.INSTANCE.convertList(pageResult.getList());
        Map<String, Object> result = new HashMap<>();
        result.put("count",pageResult.getTotal());
        result.put("score",pageResult.getTotal()>0?RandomUtil.randomDouble(4.7,4.90,2, RoundingMode.CEILING):0);
        result.put("list", CollectionUtil.getFirst(appraiseRespVOS));
        return success(result);
    }
    @GetMapping("/mini-list")
    @Operation(summary = "获得评价")
    @PermitAll
    public CommonResult<PageResult<AppraiseRespVO>> getMiniList(@Valid @NotNull Long shopId,Integer pageNo,Integer pageSize) {
        AppraisePageReqVO pageVO=new AppraisePageReqVO();
        pageVO.setShopId(shopId);
        pageVO.setPageNo(pageNo);
        pageVO.setPageSize(pageSize);
        PageResult<AppraiseDO> pageResult = appraiseService.getAppraisePage(pageVO);
        return success(AppraiseConvert.INSTANCE.convertPage(pageResult));
    }

    @PostMapping("/mini-create")
    @Operation(summary = "创建评价")
    @PermitAll
    public CommonResult<Boolean> miniCreate(@Valid @RequestBody MiniAppraiseCreateReqVO createReqVO) {
        appraiseService.createMiniAppraise(createReqVO);
        return success(true);
    }
}
